package cn.edu.ctbu.labtest.controller;

import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/teacher/findAll")
    public List<Teacher> findAll()
    {
        return teacherService.findAll();
    }

    @RequestMapping("/teacher/tlist")
    public String list(HttpServletRequest httprequest, Model model, String name){
        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //未登录，需要去登录
            return "redirect:/login/user";
        }
        log.info("name:{}",name);
        List<Teacher> teachers= teacherService.findAll();
        model.addAttribute("data",teachers);

        List<Teacher> teacher=teacherService.findByName(name);
        model.addAttribute("teacher",teacher);

        return "/teacher/tlist";
    }
}
